package com.xnyzc.lhy.mis.service.impl.cms.driver;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xnyzc.lhy.common.component.convert.DateToStringConverter;
import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.penum.cms.EDriverComingUserType;
import com.xnyzc.lhy.common.penum.work.EPunchType;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.TokenUtil;
import com.xnyzc.lhy.mis.entity.param.cms.PageVo;
import com.xnyzc.lhy.mis.entity.param.cms.driver.CmsOadUserComingParam;
import com.xnyzc.lhy.mis.entity.result.cms.driver.CmsDriverComingListRes;
import com.xnyzc.lhy.mis.entity.user.OaDUser;
import com.xnyzc.lhy.mis.entity.work.OaCommutRecord;
import com.xnyzc.lhy.mis.mapper.user.OaDUserMapper;
import com.xnyzc.lhy.mis.mapper.work.OaCommutRecordMapper;
import com.xnyzc.lhy.mis.service.cms.driver.CmsDriverComingManageService;
import com.xnyzc.lhy.mis.service.cms.util.IOaSysGetOfficeIdListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 司机通勤列表实现类
 *
 * @author zhaolijie
 * @date 2019-08-21 19:37
 */
@Service
public class CmsDriverComingManageServiceImpl implements CmsDriverComingManageService {

    @Autowired
    private IOaSysGetOfficeIdListService iOaSysGetOfficeIdListService;

    @Autowired
    private OaDUserMapper oadUserMapper;


    @Autowired
    private OaCommutRecordMapper oaCommutRecordMapper;


    /**
     * 获取司机通勤列表
     *
     * @param cmsOadUserComingParam
     * @return
     */
    @Override
    public Rv getCmsDriverComingList(CmsOadUserComingParam cmsOadUserComingParam) {
        Integer page=cmsOadUserComingParam.getPage();
        Integer pageSize=cmsOadUserComingParam.getPageSize();
        String userName=cmsOadUserComingParam.getUserName();
        String mobileAccount=cmsOadUserComingParam.getMobileAccount();
        Long sysUserId=TokenUtil.getTokenUserId();
        List<String> dataScope=TokenUtil.getRoleDataScope();
        if(CheckUtil.objIsEmpty(page)||CheckUtil.objIsEmpty(pageSize)||CheckUtil.objIsEmpty(dataScope)||CheckUtil.objIsEmpty(sysUserId)){
            throw PangException.create(EErrorCode.missingArg);
        }
        List<Long> selectDriverIdList=iOaSysGetOfficeIdListService.selectDUserIdList(sysUserId,dataScope);
        PageVo pageVo = new PageVo();
        List<CmsDriverComingListRes> resultList=new ArrayList<>();
        pageVo.setItems(resultList);
        pageVo.setTotal(0L);
        //分页查询
        IPage<OaCommutRecord> oaCommutRecordIPage=getOadDriverComingList(userName,mobileAccount,selectDriverIdList,page,pageSize);
        if(CheckUtil.objIsEmpty(oaCommutRecordIPage)){
            return Rv.wrap(pageVo);
        }
        List<Long> driverIdList=new ArrayList<>();
        //查询司机信息
        List<OaDUser> driverList=getDriverList(driverIdList);
        //设置返回结果
        CmsDriverComingListRes res=null;
        for (OaCommutRecord r:oaCommutRecordIPage.getRecords()) {
            res=new CmsDriverComingListRes();
            res.setRecordId(r.getRecordId());
            res.setPunchAddress(r.getPunchAddress());
//            res.setPunchStatus(EPunchStatus.findById(r.getPunchStatus()).getDesc());
            res.setPunchTime(new DateToStringConverter().convert(r.getPunchTime()));
            res.setPunchType(EPunchType.findById(r.getPunchType()).getDesc());
            if(CheckUtil.objIsNotEmpty(driverList)){
                for(OaDUser d:driverList){
                    if(d.getDriverId().equals(r.getUserId())){
                        res.setMobileAccount(d.getDriverPhone());
                        res.setDriverName(d.getDriverName());
                    }
                }
            }
            resultList.add(res);
        }
        pageVo.setItems(resultList);
        pageVo.setTotal(oaCommutRecordIPage.getTotal());
        //返回查询结果
        return Rv.wrap(pageVo);
    }

    /**
     * 通过姓名和手机号码查询司机信息
     * @param userName
     * @param mobielAccount
     * @return
     */
    private IPage<OaCommutRecord> getOadDriverComingList(String userName, String mobielAccount, List<Long> idList, Integer page, Integer pageSize){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.orderByDesc(OaCommutRecord.CREATE_TIME);
        queryWrapper.eq(OaCommutRecord.TAG,TokenUtil.getTag());
        queryWrapper.in(OaCommutRecord.USER_ID,idList);
        queryWrapper.eq(OaCommutRecord.USER_TYPE, EDriverComingUserType.driver.getType());
        //通过姓名查询司机信息
        if(CheckUtil.objIsNotEmpty(userName)||CheckUtil.objIsNotEmpty(mobielAccount)){
            //通过姓名查询用户信息d
            List<OaDUser> userList=getDriverList(idList,userName,mobielAccount);
            if(CheckUtil.objIsEmpty(userList)){
                return  null;
            }
            List<Long> driverIdList=new ArrayList<>();
            for(OaDUser v:userList){
                driverIdList.add(v.getDriverId());
            }
            queryWrapper.in(OaCommutRecord.USER_ID,driverIdList);
        }
        IPage<OaCommutRecord> iPage = new Page<>();
        iPage.setCurrent(page);
        iPage.setSize(pageSize);
        IPage<OaCommutRecord> oaDUsers=oaCommutRecordMapper.selectPage(iPage, queryWrapper);
        return  oaDUsers;
    }


    /**
     * 通过姓名和手机号码查询司机信息
     * @param userName
     * @param mobielAccount
     * @return
     */
    private List<OaDUser> getDriverList( List<Long> idList,String userName,String mobielAccount){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq(OaDUser.TAG,TokenUtil.getTag());
        queryWrapper.in(OaDUser.DRIVER_ID,idList);
        if(CheckUtil.objIsNotEmpty(userName)){
            //通过姓名查询司机信息
            queryWrapper.like(OaDUser.DRIVER_NAME,userName);
        }
        if(CheckUtil.objIsNotEmpty(mobielAccount)){
            //通过手机号码查询司机信息
            queryWrapper.like(OaDUser.DRIVER_PHONE,mobielAccount);
        }
        List<OaDUser> oaDUsers=oadUserMapper.selectList(queryWrapper);
        return  oaDUsers;
    }

    /**
     * 通过id查询司机信息
     * @param idList
     * @return
     */
    private List<OaDUser> getDriverList( List<Long> idList){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq(OaDUser.TAG,TokenUtil.getTag());
        queryWrapper.in(OaDUser.DRIVER_ID,idList);
        List<OaDUser> oaDUsers=oadUserMapper.selectList(queryWrapper);
        return  oaDUsers;
    }

    /**
     * 获取司机通勤列表
     *
     * @param cmsOadUserComingParam
     * @return
     */
    @Override
    public Rv exportCmsDriverComingExcel(CmsOadUserComingParam cmsOadUserComingParam) {
        String userName=cmsOadUserComingParam.getUserName();
        String mobileAccount=cmsOadUserComingParam.getMobileAccount();
        Long sysUserId=TokenUtil.getTokenUserId();
        List<String> dataScope=TokenUtil.getRoleDataScope();
        if(CheckUtil.objIsEmpty(dataScope)||CheckUtil.objIsEmpty(sysUserId)){
            throw PangException.create(EErrorCode.missingArg);
        }
        List<Long> selectDriverIdList=iOaSysGetOfficeIdListService.selectDUserIdList(sysUserId,dataScope);
        List<CmsDriverComingListRes> resultList=new ArrayList<>();
        List<OaCommutRecord> oaCommutRecordIPage=getExportDriverComingList(userName,mobileAccount,selectDriverIdList);
        if(CheckUtil.objIsEmpty(oaCommutRecordIPage)){
            return Rv.wrap(EErrorCode.success,resultList);
        }
        List<Long> driverIdList=new ArrayList<>();
        //查询司机信息
        List<OaDUser> driverList=getDriverList(driverIdList);
        //设置返回结果
        CmsDriverComingListRes res=null;
        for (OaCommutRecord r:oaCommutRecordIPage) {
            res=new CmsDriverComingListRes();
            res.setRecordId(r.getRecordId());
            res.setPunchAddress(r.getPunchAddress());
//            res.setPunchStatus(EPunchStatus.findById(r.getPunchStatus()).getDesc());
            res.setPunchTime(new DateToStringConverter().convert(r.getPunchTime()));
            res.setPunchType(EPunchType.findById(r.getPunchType()).getDesc());
            if(CheckUtil.objIsNotEmpty(driverList)){
                for(OaDUser d:driverList){
                    if(d.getDriverId().equals(r.getUserId())){
                        res.setMobileAccount(d.getDriverPhone());
                        res.setDriverName(d.getDriverName());
                    }
                }
            }
            resultList.add(res);
        }
        //返回查询结果
        return Rv.wrap(EErrorCode.success,resultList);
    }

    /**
     * 查询导出列表
     * @param userName
     * @param mobielAccount
     * @return
     */
    private List<OaCommutRecord> getExportDriverComingList(String userName, String mobielAccount, List<Long> idList){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq(OaCommutRecord.TAG,TokenUtil.getTag());
        queryWrapper.in(OaCommutRecord.USER_ID,idList);
        queryWrapper.eq(OaCommutRecord.USER_TYPE, EDriverComingUserType.driver.getType());
        //通过姓名查询司机信息
        if(CheckUtil.objIsNotEmpty(userName)||CheckUtil.objIsNotEmpty(mobielAccount)){
            //通过姓名查询用户信息d
            List<OaDUser> userList=getDriverList(idList,userName,mobielAccount);
            if(CheckUtil.objIsEmpty(userList)){
                return  null;
            }
            List<Long> driverIdList=new ArrayList<>();
            for(OaDUser v:userList){
                driverIdList.add(v.getDriverId());
            }
            queryWrapper.in(OaCommutRecord.USER_ID,driverIdList);
        }
        List<OaCommutRecord> list=oaCommutRecordMapper.selectList(queryWrapper);
        return  list;
    }

}
